#include "OverlayWindow.h"
#include "ui_OverlayWindow.h"


OverlayWindow::OverlayWindow(Settings * settings, MainWindow * mainWindow, QWidget *parent) : QMainWindow(parent),ui(new Ui::OverlayWindow){
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);

    this -> settings = settings;
    this -> mainWindow = mainWindow;
    this -> monstersWidgets = new  QList<MonsterWidget *>();
    this -> fade = new WidgetFade();

    this -> switcher = new Switcher();
    this -> isClicked = false;
    this -> isCtrlPressed = false;

    addSwitcher();
    setTransparentBackground(this->settings->getValue("transparentBackground").toBool());
    setCursorType(cursorNormal);
    move(this->settings->getValue("overlayX").toInt(),this->settings->getValue("overlayY").toInt());
    connect(this->switcher, SIGNAL(signalGameModChanged()), this, SLOT(slotGameModChanged()));
    this->setFocus();
}



void OverlayWindow::openWindow(){
    deleteMonsters();
    addMonsters();
    this->show();
    if (this->settings->getValue("fading").toBool())
         fade -> fade(0, 1);
    this->clearFocus();
}


void OverlayWindow::closeWindow(){
    deleteMonsters();
    this->close();
}


bool OverlayWindow::isCursorOverIt(QMouseEvent *event, QWidget * widget){
    if (event->x() >= widget->x() && event->x() <= widget->x() + widget->width() && event->y() >= widget->y() && event->y() <= widget->y() + widget->height())
        return true;
    else
        return false;
}


void OverlayWindow::mousePressEvent(QMouseEvent *event){
    if (!this->isCtrlPressed){
        if (event->button() == Qt::LeftButton){
            if (isCursorOverIt(event, this->switcher)){
                if (!this->fade->isFading() || true)
                    this -> switcher -> switchMod();
            }
        }
        for (int i = 0; i < this->monstersWidgets->size(); i++){
            if (isCursorOverIt(event, this->monstersWidgets->at(i))){
                if (event->button() == Qt::LeftButton){
                    this->monstersWidgets->at(i)->kill();
                } else if(event->button() == Qt::RightButton){
                    this->monstersWidgets->at(i)->setAlive(false);
                }
            }
        }
    }
    if (event->button() == Qt::LeftButton){
        isClicked = true;
    }
    this -> clickCursorX = event->globalPos().x() - this->x();
    this -> clickCursorY = event->globalPos().y() - this->y();
}

void OverlayWindow::mouseMoveEvent(QMouseEvent *event){
    if (this -> isClicked && this -> isCtrlPressed){
        this->move(event->globalPos().x() - this -> clickCursorX,event->globalPos().y() - this -> clickCursorY);
    }
}


void OverlayWindow::mouseReleaseEvent(QMouseEvent *event){
    if (event->button() == Qt::LeftButton ){
        isClicked = false;
    }
    if (event -> button() == Qt::LeftButton && !this -> isCtrlPressed){
        ChangeWindow::changeWindow("League of Legends (TM) Client");
    }


    this->settings->setValue("overlayX", this->x());
    this->settings->setValue("overlayY", this->y());

}





void OverlayWindow::keyPressEvent(QKeyEvent *event){
    if (event->key() == Qt::Key_Control){
        this->isCtrlPressed = true;
        setCursorType(cursorMove);
    }
}

void OverlayWindow::keyReleaseEvent(QKeyEvent *event){
    if (event->key() == Qt::Key_Control){
        this->isCtrlPressed = false;
        setCursorType(cursorNormal);
    }

}


void OverlayWindow::setCursorType(OverlayWindow::CursorType cursorType){
    if (cursorType == cursorNormal)
        this -> setCursor(QCursor(QPixmap(":/PNG/Resources/PNG/Cursors/normal.png"),0 ,0));
    else if (cursorType == cursorMove)
        this -> setCursor(QCursor(QPixmap(":/PNG/Resources/PNG/Cursors/move.png"),0 ,0));
}


OverlayWindow::~OverlayWindow(){
    delete ui;
    deleteMonsters();
    delete this -> monstersWidgets;
    delete this -> switcher;
    delete this -> fade;
}


void OverlayWindow::setTransparentBackground(bool value){
    if (value){
        this -> setAttribute(Qt::WA_TranslucentBackground, true);
        this -> update();
    } else{
        this -> setAttribute(Qt::WA_TranslucentBackground, false);
        this -> update();
    }
}



void OverlayWindow::addSwitcher(){
    this->switcher->setParent(this->ui->centralwidget);
    this->switcher->move(0,0);
}


void OverlayWindow::deleteMonsters(){
    setAllMonstersAlive();
    this -> fade -> clearWidgetList();

    while (!this->monstersWidgets->isEmpty()){
        delete monstersWidgets -> first();
        this -> monstersWidgets -> pop_front();
    }
}

void OverlayWindow::addMonsters(){
    if (this -> switcher -> getMod() == Switcher::mod5v5Full){
        monstersWidgets -> append(new MonsterWidget(this -> mainWindow,  new Monster(Monster::typeGolem, Monster::colorBlue, 300000)));
        monstersWidgets -> append(new MonsterWidget(this -> mainWindow,  new Monster(Monster::typeLizard, Monster::colorBlue, 300000)));
        monstersWidgets -> append(new MonsterWidget(this -> mainWindow,  new Monster(Monster::typeGolem, Monster::colorViolet, 300000)));
        monstersWidgets -> append(new MonsterWidget(this -> mainWindow,  new Monster(Monster::typeLizard, Monster::colorViolet, 300000)));
        monstersWidgets -> append(new MonsterWidget(this -> mainWindow,  new Monster(Monster::typeDrake, Monster::colorWhite, 360000)));
        monstersWidgets -> append(new MonsterWidget(this -> mainWindow,  new Monster(Monster::typeBaron, Monster::colorWhite, 900000)));
    } else if (this -> switcher -> getMod() == Switcher::mod5v5Lite){
        monstersWidgets -> append(new MonsterWidget(this -> mainWindow,  new Monster(Monster::typeDrake, Monster::colorWhite, 360000)));
        monstersWidgets -> append(new MonsterWidget(this -> mainWindow,  new Monster(Monster::typeBaron, Monster::colorWhite, 900000)));
    } else if (this -> switcher -> getMod() == Switcher::mod3v3){
        monstersWidgets -> append(new MonsterWidget(this -> mainWindow,  new Monster(Monster::typeLizard, Monster::colorWhite, 240000)));
        monstersWidgets -> append(new MonsterWidget(this -> mainWindow,  new Monster(Monster::typeDrake, Monster::colorWhite, 300000)));
    }
    this -> fade -> setWidgetList(*((QList<QWidget*> *)this -> monstersWidgets));
    for (int i = 0; i < this->monstersWidgets->size(); i++){
        this -> monstersWidgets->at(i)->setParent(this -> ui -> centralwidget);
        this -> monstersWidgets->at(i)->move(this->monstersWidgets->at(i)->width() * i + this->switcher->width(), this->switcher->height() / 2 - this->monstersWidgets->at(i)->height()/2);
        this -> monstersWidgets->at(i)->show();
    }


    this -> resize(((this->monstersWidgets->size()) ? (this->monstersWidgets->at(0)->width() * (this->monstersWidgets->size())) : (0)) + this->switcher->width(), this->switcher->height());
    this -> switcher -> updateText();
}



void OverlayWindow::setAllMonstersAlive(){
    for (int i = 0; i < this->monstersWidgets->size(); i++)
        this -> monstersWidgets -> at(i) -> setAlive();
}


void OverlayWindow::slotGameModChanged(){
    if (this->settings->getValue("fading").toBool()){
        if (!this->fade->isFading()){
            this -> fade -> fade(1,0,2);
            this -> msleep(this->fade->getTime());
            this -> fade -> stopFading();
        }
    }
    deleteMonsters();
    addMonsters();
    if (this->settings->getValue("fading").toBool())
        this -> fade -> fade(0,1,10);
}

void OverlayWindow::msleep(unsigned long msecs){
    QTimer::singleShot(msecs, this, SIGNAL(signalSleepEnd()));
    QEventLoop loop;
    QObject::connect(this, SIGNAL(signalSleepEnd()), &loop, SLOT(quit()));
    loop.exec();
}
